import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;

public class MainFrame extends JFrame {
    private JPanel mainPanel;
    private JPanel contentPanel;  // 用于显示操作界面的面板
    private List<Class> classes = new ArrayList<>();
    private List<Group> groups = new ArrayList<>();
    private List<Student> students = new ArrayList<>();
    
    public MainFrame() {
        // 设置窗口标题
        setTitle("教学管理系统");
        
        // 加载保存的数据
        DataManager.loadData(classes, groups, students);
        
        // 创建主面板
        mainPanel = new JPanel(new BorderLayout());
        
        // 创建顶部按钮面板
        JPanel buttonPanel = new JPanel(new GridLayout(1, 4, 10, 0));
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        
        // 创建内容面板
        contentPanel = new JPanel(new CardLayout());
        contentPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        
        // 创建功能按钮
        createButtons(buttonPanel);
        
        // 添加面板到主面板
        mainPanel.add(buttonPanel, BorderLayout.NORTH);
        mainPanel.add(contentPanel, BorderLayout.CENTER);
        
        // 设置窗口属性
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        add(mainPanel);
        setSize(800, 600);
        setLocationRelativeTo(null);
        
        // 添加窗口关闭事件
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                saveData();
            }
        });
    }
    
    private void createButtons(JPanel buttonPanel) {
        // 班级管理按钮
        JButton classManageBtn = createMenuButton("班级管理", e -> showClassManagement());
        
        // 小组管理按钮
        JButton groupManageBtn = createMenuButton("小组管理", e -> showGroupManagement());
        
        // 学生管理按钮
        JButton studentManageBtn = createMenuButton("学生管理", e -> showStudentManagement());
        
        // 课堂管理按钮
        JButton classroomManageBtn = createMenuButton("课堂管理", e -> showClassroomManagement());
        
        // 添加按钮到面板
        buttonPanel.add(classManageBtn);
        buttonPanel.add(groupManageBtn);
        buttonPanel.add(studentManageBtn);
        buttonPanel.add(classroomManageBtn);
    }
    
    private JButton createMenuButton(String text, ActionListener listener) {
        JButton button = new JButton(text);
        button.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        button.setPreferredSize(new Dimension(150, 40));
        button.addActionListener(listener);
        return button;
    }
    
    private void showContent(JPanel panel) {
        contentPanel.removeAll();
        contentPanel.add(panel);
        contentPanel.revalidate();
        contentPanel.repaint();
    }
    
    private void showClassManagement() {
        JPanel panel = new JPanel(new BorderLayout());
        
        // 创建选项按钮面板
        JPanel optionsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JButton addBtn = new JButton("新增班级");
        JButton editBtn = new JButton("班级维护");
        JButton listBtn = new JButton("班级列表");
        
        optionsPanel.add(addBtn);
        optionsPanel.add(editBtn);
        optionsPanel.add(listBtn);
        
        // 创建内容显示面板
        JPanel displayPanel = new JPanel(new BorderLayout());
        displayPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        
        addBtn.addActionListener(e -> {
            displayPanel.removeAll();
            
            // 创建新增班级的表单
            JPanel formPanel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(10, 10, 10, 10);
            
            JLabel nameLabel = new JLabel("班级名称：");
            nameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
            
            JTextField nameField = new JTextField(20);
            nameField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
            nameField.setPreferredSize(new Dimension(200, 35));
            
            JButton submitBtn = new JButton("确认");
            submitBtn.setFont(new Font("微软雅黑", Font.PLAIN, 16));
            submitBtn.setPreferredSize(new Dimension(100, 35));
            
            gbc.anchor = GridBagConstraints.CENTER;
            gbc.gridx = 0; gbc.gridy = 0;
            formPanel.add(nameLabel, gbc);
            
            gbc.gridx = 1;
            formPanel.add(nameField, gbc);
            
            gbc.gridx = 0; gbc.gridy = 1;
            gbc.gridwidth = 2;
            gbc.insets = new Insets(20, 10, 10, 10);
            formPanel.add(submitBtn, gbc);
            
            JPanel centerPanel = new JPanel(new GridBagLayout());
            centerPanel.add(formPanel, new GridBagConstraints());
            
            displayPanel.add(centerPanel, BorderLayout.CENTER);
            displayPanel.revalidate();
            displayPanel.repaint();
            
            submitBtn.addActionListener(ev -> {
                String className = nameField.getText().trim();
                if (!className.isEmpty()) {
                    String classId = "C" + (classes.size() + 1);
                    classes.add(new Class(classId, className));
                    saveData();
                    nameField.setText("");
                    JOptionPane.showMessageDialog(this, "班级添加成功！");
                }
            });
        });
        
        editBtn.addActionListener(e -> {
            displayPanel.removeAll();
            
            if (classes.isEmpty()) {
                JLabel msgLabel = new JLabel("暂无班级信息！");
                msgLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                msgLabel.setHorizontalAlignment(SwingConstants.CENTER);
                displayPanel.add(msgLabel, BorderLayout.CENTER);
            } else {
                JPanel formPanel = new JPanel(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.insets = new Insets(10, 10, 10, 10);
                
                JLabel selectLabel = new JLabel("选择班级：");
                selectLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                
                JComboBox<Class> classCombo = new JComboBox<>(classes.toArray(new Class[0]));
                classCombo.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                classCombo.setPreferredSize(new Dimension(200, 35));
                
                JLabel nameLabel = new JLabel("新班级名称：");
                nameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                
                JTextField nameField = new JTextField(20);
                nameField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                nameField.setPreferredSize(new Dimension(200, 35));
                
                JButton submitBtn = new JButton("确认修改");
                submitBtn.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                submitBtn.setPreferredSize(new Dimension(100, 35));
                
                // 当选择班级时自动填充当前名称
                classCombo.addActionListener(ev -> {
                    Class selectedClass = (Class) classCombo.getSelectedItem();
                    if (selectedClass != null) {
                        nameField.setText(selectedClass.getClassName());
                    }
                });
                
                submitBtn.addActionListener(ev -> {
                    Class selectedClass = (Class) classCombo.getSelectedItem();
                    String newName = nameField.getText().trim();
                    if (selectedClass != null && !newName.isEmpty()) {
                        selectedClass.setClassName(newName);
                        saveData();
                        JOptionPane.showMessageDialog(this, "修改成功！");
                        classCombo.repaint();
                    }
                });
                
                gbc.anchor = GridBagConstraints.CENTER;
                gbc.gridx = 0; gbc.gridy = 0;
                formPanel.add(selectLabel, gbc);
                
                gbc.gridx = 1;
                formPanel.add(classCombo, gbc);
                
                gbc.gridx = 0; gbc.gridy = 1;
                formPanel.add(nameLabel, gbc);
                
                gbc.gridx = 1;
                formPanel.add(nameField, gbc);
                
                gbc.gridx = 0; gbc.gridy = 2;
                gbc.gridwidth = 2;
                gbc.insets = new Insets(20, 10, 10, 10);
                formPanel.add(submitBtn, gbc);
                
                JPanel centerPanel = new JPanel(new GridBagLayout());
                centerPanel.add(formPanel, new GridBagConstraints());
                
                displayPanel.add(centerPanel, BorderLayout.CENTER);
            }
            
            displayPanel.revalidate();
            displayPanel.repaint();
        });
        
        listBtn.addActionListener(e -> {
            displayPanel.removeAll();
            
            if (classes.isEmpty()) {
                JLabel msgLabel = new JLabel("暂无班级信息！");
                msgLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                msgLabel.setHorizontalAlignment(SwingConstants.CENTER);
                displayPanel.add(msgLabel, BorderLayout.CENTER);
            } else {
                // 创建表格模型
                String[] columnNames = {"班级ID", "班级名称"};
                Object[][] data = new Object[classes.size()][2];
                for (int i = 0; i < classes.size(); i++) {
                    Class cls = classes.get(i);
                    data[i][0] = cls.getClassId();
                    data[i][1] = cls.getClassName();
                }
                
                JTable table = new JTable(data, columnNames);
                table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
                table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 14));
                table.setRowHeight(30);
                
                JScrollPane scrollPane = new JScrollPane(table);
                scrollPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
                
                displayPanel.add(scrollPane, BorderLayout.CENTER);
            }
            
            displayPanel.revalidate();
            displayPanel.repaint();
        });
        
        panel.add(optionsPanel, BorderLayout.NORTH);
        panel.add(displayPanel, BorderLayout.CENTER);
        
        showContent(panel);
    }
    
    private void showGroupManagement() {
        JPanel panel = new JPanel(new BorderLayout());
        
        // 创建选项按钮面板
        JPanel optionsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JButton addBtn = new JButton("新增小组");
        JButton editBtn = new JButton("小组维护");
        JButton listBtn = new JButton("小组列表");
        
        optionsPanel.add(addBtn);
        optionsPanel.add(editBtn);
        optionsPanel.add(listBtn);
        
        // 创建内容显示面板
        JPanel displayPanel = new JPanel(new BorderLayout());
        displayPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        
        // 新增小组
        addBtn.addActionListener(e -> {
            displayPanel.removeAll();
            
            if (classes.isEmpty()) {
                JLabel msgLabel = new JLabel("请先创建班级！");
                msgLabel.setHorizontalAlignment(SwingConstants.CENTER);
                displayPanel.add(msgLabel, BorderLayout.CENTER);
            } else {
                JPanel formPanel = new JPanel(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.insets = new Insets(10, 10, 10, 10);
                
                Font labelFont = new Font("微软雅黑", Font.PLAIN, 16);
                Font inputFont = new Font("微软雅黑", Font.PLAIN, 16);
                
                JLabel classLabel = new JLabel("选择班级：");
                classLabel.setFont(labelFont);
                
                JComboBox<Class> classCombo = new JComboBox<>(classes.toArray(new Class[0]));
                classCombo.setFont(inputFont);
                classCombo.setPreferredSize(new Dimension(200, 35));
                
                JLabel nameLabel = new JLabel("小组名称：");
                nameLabel.setFont(labelFont);
                
                JTextField nameField = new JTextField(20);
                nameField.setFont(inputFont);
                nameField.setPreferredSize(new Dimension(200, 35));
                
                JButton submitBtn = new JButton("确认");
                submitBtn.setFont(labelFont);
                submitBtn.setPreferredSize(new Dimension(100, 35));
                
                // 添加提交按钮的事件处理
                submitBtn.addActionListener(ev -> {
                    String groupName = nameField.getText().trim();
                    Class selectedClass = (Class) classCombo.getSelectedItem();
                    if (!groupName.isEmpty() && selectedClass != null) {
                        String groupId = "G" + (groups.size() + 1);
                        groups.add(new Group(groupId, groupName, selectedClass.getClassId()));
                        saveData();
                        nameField.setText("");
                        JOptionPane.showMessageDialog(this, "小组添加成功！");
                    }
                });
                
                gbc.anchor = GridBagConstraints.CENTER;
                gbc.gridx = 0; gbc.gridy = 0;
                formPanel.add(classLabel, gbc);
                
                gbc.gridx = 1;
                formPanel.add(classCombo, gbc);
                
                gbc.gridx = 0; gbc.gridy = 1;
                formPanel.add(nameLabel, gbc);
                
                gbc.gridx = 1;
                formPanel.add(nameField, gbc);
                
                gbc.gridx = 0; gbc.gridy = 2;
                gbc.gridwidth = 2;
                gbc.insets = new Insets(20, 10, 10, 10);
                formPanel.add(submitBtn, gbc);
                
                JPanel centerPanel = new JPanel(new GridBagLayout());
                centerPanel.add(formPanel, new GridBagConstraints());
                
                displayPanel.add(centerPanel, BorderLayout.CENTER);
            }
            
            displayPanel.revalidate();
            displayPanel.repaint();
        });
        
        editBtn.addActionListener(e -> {
            displayPanel.removeAll();
            
            if (groups.isEmpty()) {
                JLabel msgLabel = new JLabel("暂无小组信息！");
                msgLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                msgLabel.setHorizontalAlignment(SwingConstants.CENTER);
                displayPanel.add(msgLabel, BorderLayout.CENTER);
            } else {
                JPanel formPanel = new JPanel(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.insets = new Insets(10, 10, 10, 10);
                
                JLabel selectLabel = new JLabel("选择小组：");
                selectLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                
                JComboBox<Group> groupCombo = new JComboBox<>(groups.toArray(new Group[0]));
                groupCombo.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                groupCombo.setPreferredSize(new Dimension(200, 35));
                
                JLabel nameLabel = new JLabel("新小组名称：");
                nameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                
                JTextField nameField = new JTextField(20);
                nameField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                nameField.setPreferredSize(new Dimension(200, 35));
                
                JButton submitBtn = new JButton("确认修改");
                submitBtn.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                submitBtn.setPreferredSize(new Dimension(100, 35));
                
                // 当选择小组时自动填充当前名称
                groupCombo.addActionListener(ev -> {
                    Group selectedGroup = (Group) groupCombo.getSelectedItem();
                    if (selectedGroup != null) {
                        nameField.setText(selectedGroup.getGroupName());
                    }
                });
                
                submitBtn.addActionListener(ev -> {
                    Group selectedGroup = (Group) groupCombo.getSelectedItem();
                    String newName = nameField.getText().trim();
                    if (selectedGroup != null && !newName.isEmpty()) {
                        selectedGroup.setGroupName(newName);
                        saveData();
                        JOptionPane.showMessageDialog(this, "修改成功！");
                        groupCombo.repaint();
                    }
                });
                
                gbc.anchor = GridBagConstraints.CENTER;
                gbc.gridx = 0; gbc.gridy = 0;
                formPanel.add(selectLabel, gbc);
                
                gbc.gridx = 1;
                formPanel.add(groupCombo, gbc);
                
                gbc.gridx = 0; gbc.gridy = 1;
                formPanel.add(nameLabel, gbc);
                
                gbc.gridx = 1;
                formPanel.add(nameField, gbc);
                
                gbc.gridx = 0; gbc.gridy = 2;
                gbc.gridwidth = 2;
                gbc.insets = new Insets(20, 10, 10, 10);
                formPanel.add(submitBtn, gbc);
                
                JPanel centerPanel = new JPanel(new GridBagLayout());
                centerPanel.add(formPanel, new GridBagConstraints());
                
                displayPanel.add(centerPanel, BorderLayout.CENTER);
            }
            
            displayPanel.revalidate();
            displayPanel.repaint();
        });
        
        listBtn.addActionListener(e -> {
            displayPanel.removeAll();
            
            if (groups.isEmpty()) {
                JLabel msgLabel = new JLabel("暂无小组信息！");
                msgLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                msgLabel.setHorizontalAlignment(SwingConstants.CENTER);
                displayPanel.add(msgLabel, BorderLayout.CENTER);
            } else {
                // 创建表格模型
                String[] columnNames = {"小组ID", "小组名称", "所属班级"};
                Object[][] data = new Object[groups.size()][3];
                for (int i = 0; i < groups.size(); i++) {
                    Group group = groups.get(i);
                    String className = classes.stream()
                        .filter(c -> c.getClassId().equals(group.getClassId()))
                        .findFirst()
                        .map(Class::getClassName)
                        .orElse("未知班级");
                    data[i][0] = group.getGroupId();
                    data[i][1] = group.getGroupName();
                    data[i][2] = className;
                }
                
                JTable table = new JTable(data, columnNames);
                table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
                table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 14));
                table.setRowHeight(30);
                
                JScrollPane scrollPane = new JScrollPane(table);
                scrollPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
                
                displayPanel.add(scrollPane, BorderLayout.CENTER);
            }
            
            displayPanel.revalidate();
            displayPanel.repaint();
        });
        
        panel.add(optionsPanel, BorderLayout.NORTH);
        panel.add(displayPanel, BorderLayout.CENTER);
        
        showContent(panel);
    }
    
    private void showStudentManagement() {
        JPanel panel = new JPanel(new BorderLayout());
        
        // 创建选项按钮面板
        JPanel optionsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JButton addBtn = new JButton("新增学生");
        JButton editBtn = new JButton("学生维护");
        JButton listBtn = new JButton("学生列表");
        
        optionsPanel.add(addBtn);
        optionsPanel.add(editBtn);
        optionsPanel.add(listBtn);
        
        // 创建内容显示面板
        JPanel displayPanel = new JPanel(new BorderLayout());
        displayPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        
        // 新增学生
        addBtn.addActionListener(e -> {
            displayPanel.removeAll();
            
            if (classes.isEmpty() || groups.isEmpty()) {
                JLabel msgLabel = new JLabel("请先创建班级和小组！");
                msgLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                msgLabel.setHorizontalAlignment(SwingConstants.CENTER);
                displayPanel.add(msgLabel, BorderLayout.CENTER);
            } else {
                JPanel formPanel = new JPanel(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.insets = new Insets(10, 10, 10, 10);
                
                // 设置统一的字体
                Font labelFont = new Font("微软雅黑", Font.PLAIN, 16);
                
                JLabel classLabel = new JLabel("选择班级：");
                classLabel.setFont(labelFont);
                
                JComboBox<Class> classCombo = new JComboBox<>(classes.toArray(new Class[0]));
                classCombo.setFont(labelFont);
                classCombo.setPreferredSize(new Dimension(200, 35));
                
                JLabel groupLabel = new JLabel("选择小组：");
                groupLabel.setFont(labelFont);
                
                JComboBox<Group> groupCombo = new JComboBox<>();
                groupCombo.setFont(labelFont);
                groupCombo.setPreferredSize(new Dimension(200, 35));
                
                JLabel nameLabel = new JLabel("学生姓名：");
                nameLabel.setFont(labelFont);
                
                JTextField nameField = new JTextField(20);
                nameField.setFont(labelFont);
                nameField.setPreferredSize(new Dimension(200, 35));
                
                JButton submitBtn = new JButton("确认");
                submitBtn.setFont(labelFont);
                submitBtn.setPreferredSize(new Dimension(100, 35));
                
                // 更新小组下拉框
                classCombo.addActionListener(ev -> {
                    Class selectedClass = (Class) classCombo.getSelectedItem();
                    groupCombo.removeAllItems();
                    if (selectedClass != null) {
                        groups.stream()
                            .filter(g -> g.getClassId().equals(selectedClass.getClassId()))
                            .forEach(groupCombo::addItem);
                    }
                });
                
                // 触发一次以初始化小组下拉框
                classCombo.setSelectedIndex(0);
                
                submitBtn.addActionListener(ev -> {
                    String studentName = nameField.getText().trim();
                    Class selectedClass = (Class) classCombo.getSelectedItem();
                    Group selectedGroup = (Group) groupCombo.getSelectedItem();
                    if (!studentName.isEmpty() && selectedClass != null && selectedGroup != null) {
                        String studentId = "S" + (students.size() + 1);
                        students.add(new Student(studentId, studentName, 
                            selectedClass.getClassId(), selectedGroup.getGroupId()));
                        saveData();
                        nameField.setText("");
                        JOptionPane.showMessageDialog(this, "学生添加成功！");
                    }
                });
                
                // 使用GridBagConstraints来居中对齐
                gbc.anchor = GridBagConstraints.CENTER;
                gbc.gridx = 0; gbc.gridy = 0;
                formPanel.add(classLabel, gbc);
                
                gbc.gridx = 1;
                formPanel.add(classCombo, gbc);
                
                gbc.gridx = 0; gbc.gridy = 1;
                formPanel.add(groupLabel, gbc);
                
                gbc.gridx = 1;
                formPanel.add(groupCombo, gbc);
                
                gbc.gridx = 0; gbc.gridy = 2;
                formPanel.add(nameLabel, gbc);
                
                gbc.gridx = 1;
                formPanel.add(nameField, gbc);
                
                gbc.gridx = 0; gbc.gridy = 3;
                gbc.gridwidth = 2; // 让按钮跨越两列
                gbc.insets = new Insets(20, 10, 10, 10); // 增加按钮上方的间距
                formPanel.add(submitBtn, gbc);
                
                // 创建一个容器面板来使表单居中
                JPanel centerPanel = new JPanel(new GridBagLayout());
                centerPanel.add(formPanel, new GridBagConstraints());
                
                displayPanel.add(centerPanel, BorderLayout.CENTER);
            }
            
            displayPanel.revalidate();
            displayPanel.repaint();
        });
        
        editBtn.addActionListener(e -> {
            displayPanel.removeAll();
            
            if (students.isEmpty()) {
                JLabel msgLabel = new JLabel("暂无学生信息！");
                msgLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                msgLabel.setHorizontalAlignment(SwingConstants.CENTER);
                displayPanel.add(msgLabel, BorderLayout.CENTER);
            } else {
                JPanel formPanel = new JPanel(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.insets = new Insets(10, 10, 10, 10);
                
                JLabel selectLabel = new JLabel("选择学生：");
                selectLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                
                JComboBox<Student> studentCombo = new JComboBox<>(students.toArray(new Student[0]));
                studentCombo.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                studentCombo.setPreferredSize(new Dimension(200, 35));
                
                JLabel nameLabel = new JLabel("新学生姓名：");
                nameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                
                JTextField nameField = new JTextField(20);
                nameField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                nameField.setPreferredSize(new Dimension(200, 35));
                
                JButton submitBtn = new JButton("确认修改");
                submitBtn.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                submitBtn.setPreferredSize(new Dimension(100, 35));
                
                // 当选择学生时自动填充当前姓名
                studentCombo.addActionListener(ev -> {
                    Student selectedStudent = (Student) studentCombo.getSelectedItem();
                    if (selectedStudent != null) {
                        nameField.setText(selectedStudent.getStudentName());
                    }
                });
                
                submitBtn.addActionListener(ev -> {
                    Student selectedStudent = (Student) studentCombo.getSelectedItem();
                    String newName = nameField.getText().trim();
                    if (selectedStudent != null && !newName.isEmpty()) {
                        selectedStudent.setStudentName(newName);
                        saveData();
                        JOptionPane.showMessageDialog(this, "修改成功！");
                        studentCombo.repaint();
                    }
                });
                
                gbc.anchor = GridBagConstraints.CENTER;
                gbc.gridx = 0; gbc.gridy = 0;
                formPanel.add(selectLabel, gbc);
                
                gbc.gridx = 1;
                formPanel.add(studentCombo, gbc);
                
                gbc.gridx = 0; gbc.gridy = 1;
                formPanel.add(nameLabel, gbc);
                
                gbc.gridx = 1;
                formPanel.add(nameField, gbc);
                
                gbc.gridx = 0; gbc.gridy = 2;
                gbc.gridwidth = 2;
                gbc.insets = new Insets(20, 10, 10, 10);
                formPanel.add(submitBtn, gbc);
                
                JPanel centerPanel = new JPanel(new GridBagLayout());
                centerPanel.add(formPanel, new GridBagConstraints());
                
                displayPanel.add(centerPanel, BorderLayout.CENTER);
            }
            
            displayPanel.revalidate();
            displayPanel.repaint();
        });
        
        listBtn.addActionListener(e -> {
            displayPanel.removeAll();
            
            if (students.isEmpty()) {
                JLabel msgLabel = new JLabel("暂无学生信息！");
                msgLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                msgLabel.setHorizontalAlignment(SwingConstants.CENTER);
                displayPanel.add(msgLabel, BorderLayout.CENTER);
            } else {
                // 创建表格模型
                String[] columnNames = {"学生ID", "学生姓名", "所属班级", "所属小组", "分数"};
                Object[][] data = new Object[students.size()][5];
                for (int i = 0; i < students.size(); i++) {
                    Student student = students.get(i);
                    String className = classes.stream()
                        .filter(c -> c.getClassId().equals(student.getClassId()))
                        .findFirst()
                        .map(Class::getClassName)
                        .orElse("未知班级");
                    String groupName = groups.stream()
                        .filter(g -> g.getGroupId().equals(student.getGroupId()))
                        .findFirst()
                        .map(Group::getGroupName)
                        .orElse("未知小组");
                    data[i][0] = student.getStudentId();
                    data[i][1] = student.getStudentName();
                    data[i][2] = className;
                    data[i][3] = groupName;
                    data[i][4] = student.getAttendance();
                }
                
                JTable table = new JTable(data, columnNames);
                table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
                table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 14));
                table.setRowHeight(30);
                
                JScrollPane scrollPane = new JScrollPane(table);
                scrollPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
                
                displayPanel.add(scrollPane, BorderLayout.CENTER);
            }
            
            displayPanel.revalidate();
            displayPanel.repaint();
        });
        
        panel.add(optionsPanel, BorderLayout.NORTH);
        panel.add(displayPanel, BorderLayout.CENTER);
        
        showContent(panel);
    }
    
    private void showClassroomManagement() {
        JPanel panel = new JPanel(new BorderLayout());
        
        // 创建选项按钮面板
        JPanel optionsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JButton randomGroupBtn = new JButton("随机抽取小组");
        JButton randomStudentBtn = new JButton("随机抽取学生");
        JButton attendanceBtn = new JButton("记录考勤");
        
        optionsPanel.add(randomGroupBtn);
        optionsPanel.add(randomStudentBtn);
        optionsPanel.add(attendanceBtn);
        
        // 创建内容显示面板
        JPanel displayPanel = new JPanel(new BorderLayout());
        displayPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        
        // 随机抽取小组
        randomGroupBtn.addActionListener(e -> {
            displayPanel.removeAll();
            
            if (classes.isEmpty()) {
                JLabel msgLabel = new JLabel("请先创建班级！");
                msgLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                msgLabel.setHorizontalAlignment(SwingConstants.CENTER);
                displayPanel.add(msgLabel, BorderLayout.CENTER);
            } else {
                JPanel formPanel = new JPanel(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.insets = new Insets(10, 10, 10, 10);
                
                // 设置更大的字体
                Font labelFont = new Font("微软雅黑", Font.PLAIN, 16);
                
                JLabel classLabel = new JLabel("选择班级：");
                classLabel.setFont(labelFont);
                
                // 设置更大的下拉框
                JComboBox<Class> classCombo = new JComboBox<>(classes.toArray(new Class[0]));
                classCombo.setFont(labelFont);
                classCombo.setPreferredSize(new Dimension(200, 35));
                
                // 设置更大的按钮
                JButton randomBtn = new JButton("抽取");
                randomBtn.setFont(labelFont);
                randomBtn.setPreferredSize(new Dimension(100, 35));
                
                // 设置更大的结果标签
                JLabel resultLabel = new JLabel(" ");
                resultLabel.setFont(labelFont);
                
                randomBtn.addActionListener(ev -> {
                    Class selectedClass = (Class) classCombo.getSelectedItem();
                    if (selectedClass != null) {
                        List<Group> classGroups = groups.stream()
                            .filter(g -> g.getClassId().equals(selectedClass.getClassId()))
                            .collect(java.util.stream.Collectors.toList());
                        
                        if (classGroups.isEmpty()) {
                            resultLabel.setText("该班级下暂无小组！");
                        } else {
                            Group randomGroup = classGroups.get(new Random().nextInt(classGroups.size()));
                            resultLabel.setText("随机抽取的小组是：" + randomGroup.getGroupName());
                        }
                    }
                });
                
                // 使用GridBagConstraints来居中对齐
                gbc.anchor = GridBagConstraints.CENTER;
                gbc.gridx = 0; gbc.gridy = 0;
                formPanel.add(classLabel, gbc);
                
                gbc.gridx = 1;
                formPanel.add(classCombo, gbc);
                
                gbc.gridx = 0; gbc.gridy = 1;
                gbc.gridwidth = 2; // 让按钮跨越两列
                gbc.insets = new Insets(20, 10, 10, 10); // 增加按钮上方的间距
                formPanel.add(randomBtn, gbc);
                
                gbc.gridy = 2;
                formPanel.add(resultLabel, gbc);
                
                // 创建一个容器面板来使表单居中
                JPanel centerPanel = new JPanel(new GridBagLayout());
                centerPanel.add(formPanel, new GridBagConstraints());
                
                displayPanel.add(centerPanel, BorderLayout.CENTER);
            }
            
            displayPanel.revalidate();
            displayPanel.repaint();
        });
        
        // 随机抽取学生
        randomStudentBtn.addActionListener(e -> {
            displayPanel.removeAll();
            
            if (classes.isEmpty()) {
                JLabel msgLabel = new JLabel("请先创建班级！");
                msgLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                msgLabel.setHorizontalAlignment(SwingConstants.CENTER);
                displayPanel.add(msgLabel, BorderLayout.CENTER);
            } else {
                JPanel formPanel = new JPanel(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.insets = new Insets(10, 10, 10, 10);
                
                JLabel classLabel = new JLabel("选择班级：");
                classLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                
                JComboBox<Class> classCombo = new JComboBox<>(classes.toArray(new Class[0]));
                classCombo.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                classCombo.setPreferredSize(new Dimension(200, 35));
                
                JButton randomBtn = new JButton("抽取");
                randomBtn.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                randomBtn.setPreferredSize(new Dimension(100, 35));
                
                JLabel resultLabel = new JLabel(" ");
                resultLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                
                randomBtn.addActionListener(ev -> {
                    Class selectedClass = (Class) classCombo.getSelectedItem();
                    if (selectedClass != null) {
                        List<Student> classStudents = students.stream()
                            .filter(s -> s.getClassId().equals(selectedClass.getClassId()))
                            .collect(java.util.stream.Collectors.toList());
                        
                        if (classStudents.isEmpty()) {
                            resultLabel.setText("该班级下暂无学生！");
                        } else {
                            Student randomStudent = classStudents.get(new Random().nextInt(classStudents.size()));
                            String groupName = groups.stream()
                                .filter(g -> g.getGroupId().equals(randomStudent.getGroupId()))
                                .findFirst()
                                .map(Group::getGroupName)
                                .orElse("未知小组");
                            resultLabel.setText("随机抽取的学生是：" + randomStudent.getStudentName() + 
                                " (" + selectedClass.getClassName() + " - " + groupName + ")");
                        }
                    }
                });
                
                gbc.anchor = GridBagConstraints.CENTER;
                gbc.gridx = 0; gbc.gridy = 0;
                formPanel.add(classLabel, gbc);
                
                gbc.gridx = 1;
                formPanel.add(classCombo, gbc);
                
                gbc.gridx = 0; gbc.gridy = 1;
                gbc.gridwidth = 2;
                formPanel.add(randomBtn, gbc);
                
                gbc.gridy = 2;
                formPanel.add(resultLabel, gbc);
                
                JPanel centerPanel = new JPanel(new GridBagLayout());
                centerPanel.add(formPanel, new GridBagConstraints());
                
                displayPanel.add(centerPanel, BorderLayout.CENTER);
            }
            
            displayPanel.revalidate();
            displayPanel.repaint();
        });
        
        // 记录考勤
        attendanceBtn.addActionListener(e -> {
            displayPanel.removeAll();
            
            if (students.isEmpty()) {
                JLabel msgLabel = new JLabel("暂无学生信息！");
                msgLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                msgLabel.setHorizontalAlignment(SwingConstants.CENTER);
                displayPanel.add(msgLabel, BorderLayout.CENTER);
            } else {
                JPanel formPanel = new JPanel(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.insets = new Insets(10, 10, 10, 10);
                
                JLabel studentLabel = new JLabel("选择学生：");
                studentLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                
                JComboBox<Student> studentCombo = new JComboBox<>(students.toArray(new Student[0]));
                studentCombo.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                studentCombo.setPreferredSize(new Dimension(200, 35));
                
                JLabel scoreLabel = new JLabel("输入分数：");
                scoreLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                
                JTextField scoreField = new JTextField(20);
                scoreField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                scoreField.setPreferredSize(new Dimension(200, 35));
                
                JButton submitBtn = new JButton("确认");
                submitBtn.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                submitBtn.setPreferredSize(new Dimension(100, 35));
                
                // 当选择学生时显示当前分数
                studentCombo.addActionListener(ev -> {
                    Student selectedStudent = (Student) studentCombo.getSelectedItem();
                    if (selectedStudent != null) {
                        scoreField.setText(String.valueOf(selectedStudent.getAttendance()));
                    }
                });
                
                submitBtn.addActionListener(ev -> {
                    Student selectedStudent = (Student) studentCombo.getSelectedItem();
                    String scoreText = scoreField.getText().trim();
                    if (selectedStudent != null && !scoreText.isEmpty()) {
                        try {
                            double score = Double.parseDouble(scoreText);
                            selectedStudent.setAttendance(score);
                            saveData();
                            JOptionPane.showMessageDialog(this, "分数记录成功！");
                        } catch (NumberFormatException ex) {
                            JOptionPane.showMessageDialog(this, "请输入有效的数字！", "输入错误", 
                                JOptionPane.ERROR_MESSAGE);
                        }
                    }
                });
                
                gbc.anchor = GridBagConstraints.CENTER;
                gbc.gridx = 0; gbc.gridy = 0;
                formPanel.add(studentLabel, gbc);
                
                gbc.gridx = 1;
                formPanel.add(studentCombo, gbc);
                
                gbc.gridx = 0; gbc.gridy = 1;
                formPanel.add(scoreLabel, gbc);
                
                gbc.gridx = 1;
                formPanel.add(scoreField, gbc);
                
                gbc.gridx = 0; gbc.gridy = 2;
                gbc.gridwidth = 2;
                gbc.insets = new Insets(20, 10, 10, 10);
                formPanel.add(submitBtn, gbc);
                
                JPanel centerPanel = new JPanel(new GridBagLayout());
                centerPanel.add(formPanel, new GridBagConstraints());
                
                displayPanel.add(centerPanel, BorderLayout.CENTER);
            }
            
            displayPanel.revalidate();
            displayPanel.repaint();
        });
        
        panel.add(optionsPanel, BorderLayout.NORTH);
        panel.add(displayPanel, BorderLayout.CENTER);
        
        showContent(panel);
    }
    
    private void saveData() {
        DataManager.saveData(classes, groups, students);
    }
    
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        SwingUtilities.invokeLater(() -> {
            MainFrame frame = new MainFrame();
            frame.setVisible(true);
        });
    }
} 